//
//  GSUser.m
//  AgileNYC
//
//  Created by German on 15.06.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "GSUser.h"
#import "SGSServerManagerConsts.h"
#import "RKDotNetDateFormatter.h"
#import "SGSUtils.h"


@implementation GSUser

@dynamic city;
@dynamic company;
@dynamic deviceID;
@dynamic deviceOS;
@dynamic eMail;
@dynamic firstName;
@dynamic imageURL;
@dynamic isChanged;
@dynamic isMyProfile;
@dynamic isRegistered;
@dynamic isSynchronized;
@dynamic isdeleted;
@dynamic isTemp;
@dynamic lastName;
@dynamic memo;
@dynamic mobileWorkingSystem;
@dynamic modifiedDate;
@dynamic notes;
@dynamic password;
@dynamic phone;
@dynamic sessionID;
@dynamic socialID;

@dynamic facebookId;
@dynamic facebookName;
@dynamic googlePlusId;
@dynamic googlePlusName;
@dynamic linkedInId;
@dynamic linkedInName;

@dynamic socialType;
@dynamic state;
@dynamic title;

@dynamic userId;
@dynamic webSite;
@dynamic photo;

@dynamic tokenAgile;
@dynamic tokenFacebook;
@dynamic tokenGooglePlus;
@dynamic tokenLinkedIn;

@synthesize emptyImage = _emptyImage;
@synthesize imageWasLoaded = _imageWasLoaded;

-(id)init {
    self = [super init];
    
    if (self) {
        _imageWasLoaded = NO;
    }
    
    return self;
}

-(void) completeWithServerResponse: (NSDictionary*) response {
    
    for (NSString * key in [response allKeys]) {
        
        if ([key isEqualToString:userFirstNameKey]) {
            self.firstName       = [response objectForKey:userFirstNameKey];
        } else if ([key isEqualToString:userLastNameKey]) {
            self.lastName       = [response objectForKey:key];
        } else if ([key isEqualToString:userEmailKey]) {
            self.eMail          = [response objectForKey:userEmailKey];
        } else if ([key isEqualToString:userPasswordKey]) {
            self.password       = [response objectForKey:userPasswordKey];
        } else if ([key isEqualToString:userPhoneKey]) {
            self.phone          = [response objectForKey:userPhoneKey];
        } else if ([key isEqualToString:userTitleKey]) {
            self.title          = [response objectForKey:userTitleKey];
        } else if ([key isEqualToString:userCompanyNameKey]) {
           self.company        = [response objectForKey:userCompanyNameKey];
        } else if ([key isEqualToString:userWebSiteKey]) {
            self.webSite        = [response objectForKey:userWebSiteKey];
        } else if ([key isEqualToString:userCityKey]) {
            self.city           = [response objectForKey:userCityKey];
        } else if ([key isEqualToString:userStateKey]) {
           self.state          = [response objectForKey:userStateKey];
        } else if ([key isEqualToString:userMemoKey]) {
           self.memo           = [response objectForKey:userMemoKey];
        } else if ([key isEqualToString:userNotesKey]) {
           self.notes          = [response objectForKey:userNotesKey];
        } else if ([key isEqualToString:userSessionIDKey]) {
            self.sessionID      = [NSString stringWithFormat:@"%@",[response objectForKey:userSessionIDKey]];
        } else if ([key isEqualToString:userPhotoKey]) {
            self.imageURL       = [response objectForKey:userPhotoKey];
        } else if ([key isEqualToString:userFacebookIdKey]) {
            self.facebookId     = [response objectForKey:userFacebookIdKey];
        } else if ([key isEqualToString:userGoogleIdKey]) {
            self.googlePlusId   = [response objectForKey:userGoogleIdKey];
        } else if ([key isEqualToString:userLinkedInIdKey]) {
            self.linkedInId     = [response objectForKey:userLinkedInIdKey];
        } else if ([key isEqualToString:userFacebookNameKey]) {
            self.facebookName     = [response objectForKey:userFacebookNameKey];
        } else if ([key isEqualToString:userGoogleNameKey]) {
            self.googlePlusName   = [response objectForKey:userGoogleNameKey];
        } else if ([key isEqualToString:userLinkedInNameKey]) {
            self.linkedInName     = [response objectForKey:userLinkedInNameKey];
        }
    }
    
    self.imageURL = [NSString stringWithFormat:@"%@%@%@",agileURL,photoPostfix,self.sessionID];
    
    GSLog(GSLogInfo, @"UserImageURL: %@", self.imageURL);
    
    //self.photo = nil;

    RKDotNetDateFormatter * dtFormat = [RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    
    self.modifiedDate   = [dtFormat dateFromString:[response objectForKey:modifiedDateKey]];
}

-(NSDictionary*) returnDictionaryForAgileServer {
 
    NSMutableDictionary * result = [NSMutableDictionary dictionary];
     
    if ((self.eMail) && ([self.eMail length]>0)) [result setObject:self.eMail forKey:userEmailKey];
    if ((self.tokenAgile) && ([self.tokenAgile length]>0)) [result setObject:self.tokenAgile forKey:userTokenKey];
    if ((self.firstName) && ([self.firstName length]>0)) [result setObject:self.firstName forKey:userFirstNameKey];
    if ((self.lastName) && ([self.lastName length]>0)) [result setObject:self.lastName forKey:userLastNameKey];
    if ((self.eMail) && ([self.eMail length]>0)) [result setObject:self.eMail forKey:userEmailKey];
    if ((self.password) && ([self.password length]>0)) [result setObject:self.password forKey:userPasswordKey];
    if ((self.phone) && ([self.phone length]>0)) [result setObject:self.phone forKey:userPhoneKey];
    if ((self.title) && ([self.title length]>0)) [result setObject:self.title forKey:userTitleKey];
    if ((self.company) && ([self.company length]>0)) [result setObject:self.company forKey:userCompanyNameKey];
    if ((self.webSite) && ([self.webSite length]>0)) [result setObject:self.webSite forKey:userWebSiteKey];
    if ((self.city) && ([self.city length]>0)) [result setObject:self.city forKey:userCityKey];
    if ((self.state) && ([self.state length]>0)) [result setObject:self.state forKey:userStateKey];
    if ((self.memo) && ([self.memo length]>0)) [result setObject:self.memo forKey:userMemoKey];
    if ((self.facebookId) && ([self.facebookId length]>0)) [result setObject:self.facebookId forKey:userFacebookIdKey];
    if ((self.googlePlusId) && ([self.googlePlusId length]>0)) [result setObject:self.googlePlusId forKey:userGoogleIdKey];
    if ((self.linkedInId) && ([self.linkedInId length]>0)) [result setObject:self.linkedInId forKey:userLinkedInIdKey];
    if ((self.facebookName) && ([self.facebookName length]>0)) [result setObject:self.facebookName forKey:userFacebookNameKey];
    if ((self.googlePlusName) && ([self.googlePlusName length]>0)) [result setObject:self.googlePlusName forKey:userGoogleNameKey];
    if ((self.linkedInName) && ([self.linkedInName length]>0)) [result setObject:self.linkedInName forKey:userLinkedInNameKey];
    
    return result;
}

-(void) setPhone:(NSString *)phone {
    
    NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    NSString * onlyNumbers = [[[NSString stringWithFormat:@"%@",phone] componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString: @""];;
    
    [self willChangeValueForKey:@"phone"];
    [self setPrimitiveValue:onlyNumbers forKey:@"phone"];
    [self didChangeValueForKey:@"phone"];
}

-(NSString*) fullName {
    NSMutableString * result = [NSMutableString stringWithFormat:@""];
    
    if ((self.firstName) && ([self.firstName length]>0)) {
        [result appendString:self.firstName];
    }
    
    if (([result length]>0) && (self.lastName) && ([self.lastName length]>0)) {
        [result appendString:[NSString stringWithFormat:@" "]];
    }
    
    if ((self.lastName) && ([self.lastName length]>0)) {
        [result appendString:self.lastName];
    }

    return result;
}

-(void)setPhoto:(NSData *)photo {
    [self willChangeValueForKey:@"phone"];
    [self setPrimitiveValue:photo forKey:@"photo"];
    [self didChangeValueForKey:@"phone"];
}



-(NSString*) location {
    NSMutableString * location = [NSMutableString stringWithFormat:@""];
    
    if ((self.city) && ([self.city length]>0)) {
        [location appendString:self.city];
    }

    if (([location length]>0) && (self.state) && ([self.state length]>0)) {
        [location appendString:[NSString stringWithFormat:@", "]];
    }
    
    if ((self.state) && ([self.state length]>0)) {
        [location appendString:self.state];
    }

    return [location stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

-(NSString*) companyAndPosition {
    NSMutableString * result = [NSMutableString stringWithFormat:@""];
    
    if ((self.title) && ([self.title length]>0)) {
        [result appendString:self.title];
    }
    
    if (([result length]>0) && (self.company) && ([self.company length]>0)) {
        [result appendString:@" at "];
    }
    
    if ((self.company) && ([self.company length]>0)) {
        [result appendString:self.company];
    }

    return [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

-(NSString*) allUserData {
    return [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ %@ %@",
            self.firstName,
            self.lastName,
            self.eMail,
            self.company,
            self.title,
            self.city,
            self.state,
            self.webSite,
            self.notes,
            self.memo];
}

-(void) setPhotoAsImage:(UIImage*) photo {
    if (photo) {
        self.photo = UIImagePNGRepresentation(photo);
    } else {
        self.photo = nil;
    }
}

-(UIImage*) getPhotoAsImage {
    if (self.photo) {
        return [UIImage imageWithData:self.photo];
    } else {
        return nil;
    }
}

-(void) setEmptyImage:(UIImage*) image {
    _emptyImage = image;
}

-(UIImage*) emptyImage {
    return _emptyImage;
}

-(void) setImageWasLoaded:(BOOL)imageWasLoaded {
    _imageWasLoaded = imageWasLoaded;
}

-(BOOL)imageWasLoaded{
    return _imageWasLoaded;
}

@end
